package cn.xianyang.system.controller;

import cn.xianyang.basic.util.AjaxResult;
import cn.xianyang.basic.util.PageList;
import cn.xianyang.system.domain.Systemdictionarydetail;
import cn.xianyang.system.query.SystemdictionarydetailQuery;
import cn.xianyang.system.service.ISystemdictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class SystemdictionarydetailController {
    @Autowired
    private ISystemdictionarydetailService service;

    /**
     * 获取全部数据
     */
    @GetMapping
    public List<Systemdictionarydetail> queryAll() {
        return service.queryAll();
    }

    /**
     * 根据id获取
     */
    @GetMapping("/{id}")
    public Systemdictionarydetail queryById(@PathVariable Long id) {
        return service.queryById(id);
    }

    /**
     * 高级查询分页
     */
    @PostMapping
    public PageList<Systemdictionarydetail> queryPage(@RequestBody SystemdictionarydetailQuery systemdictionarydetailQuery) {
        return service.queryPage(systemdictionarydetailQuery);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            service.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /**
     * 根据id删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            service.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /**
     * 新增或修改
     */
    @PutMapping
    public AjaxResult AddOrUpdate(@RequestBody Systemdictionarydetail systemdictionarydetail) {
        try {
            if (systemdictionarydetail.getId() != null) {
                service.update(systemdictionarydetail);
            } else {
                service.save(systemdictionarydetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /**
     * 根据类型id查询和高级查询和分页
     */
    @PostMapping("/type")
    public PageList<Systemdictionarydetail> getByTypeId(@RequestBody SystemdictionarydetailQuery systemdictionarydetailQuery) {
        return service.loadByTypeId(systemdictionarydetailQuery);
    }
}